local guiji = fk.CreateSkill {
  name = "guiji",
}

guiji:addEffect("active", {
  anim_type = "support",
  target_num = 1,
  prompt = "#guiji-prompt",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 and not player:isKongcheng()
  end,
  card_filter = Util.FalseFunc,
  target_filter =function (self, player, to_select, selected, selected_cards)
    local target = to_select
    return player ~= to_select and target:isMale() and #selected == 0 and target:getHandcardNum() < player:getHandcardNum() 
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    U.swapHandCards(room, player, player, to, self.name)
    local mark = to:getMark("@@guiji")
    if mark == 0 then mark = {} end
    table.insertIfNeed(mark, player.id)
    room:setPlayerMark(to, "@@guiji", mark)
  end,
})


guiji:addEffect(fk.EventPhaseEnd, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target.dead or player.dead then return false end
    local mark = target:getMark("@@guiji")
    if type(mark) == "table" and table.contains(mark, player.id) and target.phase == Player.Play then
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player,{skill_name=self.name,prompt="#guiji-invoke::"..target.id})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, guiji.name)
    player:broadcastSkillInvoke(guiji.name)
    room:doIndicate(player.id, {target.id})
    U.swapHandCards(room, player, player, target, self.name)
    local mark = target:getMark("@@guiji")
    if type(mark) == "table" and table.removeOne(mark, player.id) then
      room:setPlayerMark(target, "@@guiji", #mark > 0 and mark or 0)
    end
  end,

})
guiji:addEffect(fk.EventPhaseChanging, {
  can_refresh = function(self, event, target, player, data)
    return player == target and target:getMark("@@guiji") ~= 0 and data.phase == Player.Play
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(target, "@@guiji", 0)
  end,
})


return guiji